package com.gomyb.admin.module.product.controller;

import com.gomyb.admin.module.product.model.AppProduct;
import com.gomyb.admin.module.product.model.AppProductRepertory;
import com.gomyb.admin.module.product.service.AppProductRepertoryService;
import com.gomyb.admin.module.product.service.AppProductService;
import com.gomyb.common.base.BaseController;
import com.gomyb.common.base.BaseService;
import com.gomyb.common.page.PageParam;
import com.gomyb.common.page.PageResult;
import com.gomyb.result.JsonResult;
import com.gomyb.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 */
@RestController
@Log4j2
@RequestMapping(value = "/appProduct", name = "产品")
public class AppProductController extends BaseController<AppProduct, Long> {

    @Override
    @Autowired
    public void setService(BaseService<AppProduct, Long> service) {
        super.service = service;
    }

    @Autowired
    private AppProductRepertoryService AppProductRepertoryService;

    @PostMapping(value = "/see/{id}", name = "查看")
    public Result<AppProduct> see(@PathVariable("id") Long id) {
        return super.get(id);
    }

    @PostMapping(value = "/add", name = "添加")
    public Result add(AppProduct appProduct) {
        Result<AppProduct> save = super.save(appProduct);
        if (save.isSuccess()) {
            AppProductRepertory repertory = new AppProductRepertory();
            repertory.setCount(BigDecimal.ZERO);
            repertory.setProductId(save.getData().getId());
            AppProductRepertory result = AppProductRepertoryService.save(repertory);
            if (null != result) {
                return JsonResult.OK();
            }
        }
        return JsonResult.NO();
    }

    @PostMapping(value = "/modify", name = "修改")
    public Result modify(AppProduct appProduct) {
        return super.update(appProduct);
    }


    @PostMapping(value = "/list", name = "列表")
    public PageResult<AppProduct> list(HttpServletRequest request) {
        PageParam pageParam = PageParam.getPageParam(request);
        return ((AppProductService) service).findPageBySql(pageParam);
    }

    @PostMapping(value = "/remove", name = "删除")
    public Result remove(String ids) {
        return super.deleteAll(ids);
    }


}
